using System;
using System.Collections.Generic;
using RMH.DataAccess;
using RMH.Entities;

namespace RMH.Services
{
    public class PatientService : BaseRepositoryBackedService<Patient, IPatientRepository>, IPatientService
    {
        private IRelationshipRepository relationShipRepository;
        public PatientService(IPatientRepository patientRepository, IRelationshipRepository relationShipRepository) 
            : base(patientRepository)
        {
            this.relationShipRepository = relationShipRepository;
        }

        public IList<Patient> Search(string firstName, string lastName)
        {
            var searchCriteria = new Dictionary<string, object>();
            if (!String.IsNullOrEmpty(firstName))
                searchCriteria.Add("FirstName", firstName);
            if(!String.IsNullOrEmpty(lastName))
                searchCriteria.Add("LastName", lastName);
            return _repository.FindAll(searchCriteria);
        }

        public override void Save(Patient patient)
        {
            foreach (var relationship in patient.Relationships)
            {
                if(relationship.Id == 0)
                    relationShipRepository.Save(relationship);
            }
            _repository.Save(patient);            
        }
    }
}